package com.luhongzhe.tool;

import data.ObjectPath;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Path.Direction;

/**
 * @author 	luhongzhe
 * @email  	azhe0660@gmail.com
 * @date   	2011-10-13
 */

public class PathParser
{
	public static final int C_MOVETO = 0x0;
	public static final int C_LINETO = 0x1;
	public static final int C_CLOSE = 0x2;
	public static final int C_ARCTO = 0x3;
	public static final int C_QUADBEZTO = 0x4;
	public static final int C_CUBICBEZTO = 0x5;	
	
	public static ObjectPath createPath(int[][] presetShapePathList)
	{
		ObjectPath objectPath = new ObjectPath();

		int pathCommand = 0; // 路径命令
		float currX = 0; // 当前笔触的x坐标
		float currY = 0; // 当前笔触的y坐标

		int pathListSize = presetShapePathList.length;
		objectPath.initSize(pathListSize);
		for (int j = 0; j < pathListSize; ++j)
		{
			int[] presetShapePath = presetShapePathList[j];

			Path path = new Path();
			objectPath.strokeColors[j] = presetShapePath[0];
			objectPath.fillColors[j] = presetShapePath[1];
			
			int len = presetShapePath.length;
			int i = 2;
			while (i < len)
			{
				pathCommand = presetShapePath[i];
				switch (pathCommand)
				{
					case C_MOVETO:
						currX = presetShapePath[i + 1];
						currY = presetShapePath[i + 2];
						path.moveTo(currX, currY);
						i += 3;
						break;
	
					case C_LINETO:
						currX = presetShapePath[i + 1];
						currY = presetShapePath[i + 2];
						path.lineTo(currX, currY);
						i += 3;
						break;
	
//					case C_ARCTO:
//						float width = preData.getValue(presetShapePath[i + 1]);// 圆弧所在椭圆长轴
//						float height = preData.getValue(presetShapePath[i + 2]);// 圆弧所在椭圆短轴
//						float startAngle = preData.getValue(presetShapePath[i + 3]) / 60000;// 圆弧起始点的角度
//						float sweepAngle = preData.getValue(presetShapePath[i + 4]) / 60000;// 圆弧扫过的角度
//	
//						PointF point = _addArcToPath(currX, currY, width, height,
//								startAngle, sweepAngle, graphicsPath);
//						if (point != null)
//						{
//							currX = point.x;
//							currY = point.y;
//						}
//	
//						i += 5;
//						break;
//	
//					case C_QUADBEZTO:
//						float ctrlX = preData.getValue(presetShapePath[i + 1]);
//						float ctrlY = preData.getValue(presetShapePath[i + 2]);
//						currX = preData.getValue(presetShapePath[i + 3]);
//						currY = preData.getValue(presetShapePath[i + 4]);
//						
//						pathElem = PathElem.createQuadElem(ctrlX, ctrlY, currX,currY);
//						graphicsPath.addElem(pathElem);
//						
//						i += 5;
//						break;
//	
//					case C_CUBICBEZTO:
//						float ctrlX1 = preData.getValue(presetShapePath[i + 1]);
//						float ctrlY1 = preData.getValue(presetShapePath[i + 2]);
//						float ctrlX2 = preData.getValue(presetShapePath[i + 3]);
//						float ctrlY2 = preData.getValue(presetShapePath[i + 4]);
//						currX = preData.getValue(presetShapePath[i + 5]);
//						currY = preData.getValue(presetShapePath[i + 6]);
//						pathElem = PathElem.createCubicElem(ctrlX1, ctrlY1, ctrlX2, ctrlY2, currX, currY);
//						graphicsPath.addElem(pathElem);
//						i += 7;
//						break;
	
					case C_CLOSE:
						path.close();
						i += 1;
						break;
					case C_ARCTO:
						float left = presetShapePath[i + 1];
						float top = presetShapePath[i + 2];
						float right = presetShapePath[i + 3];
						float bottom = presetShapePath[i + 4];
						float startAngle = presetShapePath[i + 5];
						float sweepAngle = presetShapePath[i + 6];
						if (sweepAngle != 360)
							path.arcTo(new RectF(left, top, right, bottom), startAngle, sweepAngle);
						else
							path.addOval(new RectF(left, top, right, bottom), Direction.CW);
						i += 7;
						break;
				}
			}
			objectPath.paths[j] = path;
		}

		return objectPath;
	}
	
	public static ObjectPath getCar()
	{
		return createPath(_car);
	}
	
	public static final int[][] _car = new int[][]{
		new int[]{0, Color.BLUE,
				C_ARCTO, 20, 90, 30, 100, 0, 360},
		new int[]{0, Color.BLUE,
				C_ARCTO, 70, 90, 80, 100, 0, 360},
		new int[]{0, Color.BLUE,
		C_MOVETO, 0, 70,  
		C_LINETO, 0, 90, C_LINETO, 10, 90, C_LINETO, 20, 80,
		C_LINETO, 30, 80, C_LINETO, 40, 90,
		C_LINETO, 60, 90, C_LINETO, 70, 80,
		C_LINETO, 80, 80, C_LINETO, 90, 90,
		C_LINETO, 100, 90, C_LINETO, 100, 70,
		C_LINETO, 70, 70, C_LINETO, 70, 60,
		C_LINETO, 65, 60, C_LINETO, 65, 70, 
		C_CLOSE},
		new int[]{0, Color.WHITE,
				C_MOVETO, 35, 70,  
				C_LINETO, 35, 60, C_LINETO, 50, 60,
				C_LINETO, 50, 70,
				C_CLOSE}
		};
		
}
